package cn.billy.org.controller;

import cn.billy.basic.utils.AjaxResult;
import cn.billy.basic.utils.PageList;
import cn.billy.org.domain.Department;
import cn.billy.org.domain.Employee;
import cn.billy.org.query.DepartmentQuery;
import cn.billy.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Employee getById(@ApiParam(value="员工主键",required = true)  @PathVariable("id") Long id){
        return iEmployeeService.getById(id);
    }

    @GetMapping
    public List<Employee> loadAll(){
        return iEmployeeService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iEmployeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        System.out.println(employee);
        try {
            if (employee.getId() == null){
                iEmployeeService.save(employee);

            } else {
                iEmployeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }

    @PostMapping
    public PageList<Employee> queryData(@RequestBody DepartmentQuery departmentQuery){
        System.out.println(departmentQuery);
        PageList<Employee> data = iEmployeeService.queryData(departmentQuery);
        return data;
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            iEmployeeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
    @GetMapping("/list/{id}")
    public List<Employee> getByUserId(@PathVariable("id") Long id){
        return iEmployeeService.getByUserId(id);
    }
}
